﻿using System;
using System.Data;

using System.Windows.Forms;
using HRM.HRMServiceProxy;

namespace HRM
{
    public partial class frmAdvancePaymentInfo : Form
    {
        int _AdvanceID;
        BranchClient _wcfBranch;
        DepartmentClient _wcfDP;
        EmployeeClient _wcfEmployee;
        PositionDetailClient _wcfPD;
        AdvancePaymentClient _wcfAdvance;
        AdvancePaymentInfo _AdvanceInfo;
        DialogResult dlgResult;
        RoleRightInfo listRole;
        bool isChange = false;
        public frmAdvancePaymentInfo(int AdvanceID, int EmployeeID)
        {
            InitializeComponent();
            LoadLanguage();
            _wcfAdvance = Service.AdvancePayment();
            _AdvanceID = AdvanceID;
            _AdvanceInfo = new AdvancePaymentInfo();
            dlgResult = DialogResult.Cancel;
            LoadBranch();
            listRole = UIParamater.Quyen_TamUngLuong;
            if (_AdvanceID > 0)
            {
                _AdvanceInfo = _wcfAdvance.AdvancePayment_GetInfo(_AdvanceID);
                Info2Form();
            }
        }

        public void LoadLanguage()
        {
            this.Text = UpdateUI.GetString(this.Tag.ToString());
            UpdateUI.Load_Panel(ref panel2);
            UpdateUI.Load_ToolStrip(ref toolStrip1);
        }

        private void LoadBranch()
        {
            _wcfBranch = Service.Branch();
            cboBranch.DisplayMember = "Branch_Name";
            cboBranch.ValueMember = "Branch_ID";
            if (!Program.isAdmin && !Program.isViewAll)
            {
                cboBranch.DataSource = _wcfBranch.Branch_GetOne(Program.intBranchID).Tables[0];
            }
            else
                cboBranch.DataSource = _wcfBranch.Branch_GetAll().Tables[0];
            _wcfBranch.Close();
        }

        private void LoadDepartment(int BranchID)
        {
            _wcfDP = Service.Department();
            cboDepartment.DisplayMember = "Department_Name";
            cboDepartment.ValueMember = "Department_ID";
            cboDepartment.DataSource = _wcfDP.Department_GetWithBranch(BranchID, 1).Tables[0];
            _wcfDP.Close();
        }

        private void LoadEmployee(int DepartmentID)
        {
            _wcfEmployee = Service.Employee();
            cboEmployee.DisplayMember = "Fullname";
            cboEmployee.ValueMember = "Employee_ID";
            cboEmployee.DataSource = _wcfEmployee.Employee_GetWithDepartment(DepartmentID).Tables[0];
            if (cboEmployee.Items.Count == 0)
                cboEmployee.Text = "";
            _wcfEmployee.Close();
        }

        private void loadPosition(int EmployeeID)
        {
            _wcfPD = Service.PositionDetail();
            EmployeeID = Convert.ToInt32(cboEmployee.SelectedValue);
            DataTable dt = _wcfPD.PositionDetail_GetWithEmployee(EmployeeID).Tables[0];
            foreach (DataRow dr in dt.Rows)
            {
                txtPosition.Text = dr["PositionName"].ToString();
            }
            _wcfPD.Close();
        }

        private void Info2Form()//Hiển thị thông tin ra form
        {
            cboBranch.SelectedValue = _AdvanceInfo.BranchID;
            cboDepartment.SelectedValue = _AdvanceInfo.DepartmentID;
            cboEmployee.SelectedValue = _AdvanceInfo.EmployeeID;
            txtPosition.Text = _AdvanceInfo.Position;
            txtDate.Text = _AdvanceInfo.Date.ToString("dd/MM/yyyy");
            txtAmount.Text = _AdvanceInfo.Amount.ToString(UIParamater.Phay2So);
            chkDinhky.Checked = _AdvanceInfo.DinhKy;
            txtAmountMonth.Text = _AdvanceInfo.AmountPayment.ToString(UIParamater.Phay2So);
            txtMonth.Text = _AdvanceInfo.Month.ToString();
            txtDecription.Text = _AdvanceInfo.Description;
            isChange = false;
        }

        private void Form2Info()//Chuyển dữ liệu từ form nhập vào Info
        {
            _AdvanceInfo.ID = _AdvanceID;
            _AdvanceInfo.BranchID = Convert.ToInt32(cboBranch.SelectedValue);
            _AdvanceInfo.DepartmentID = Convert.ToInt32(cboDepartment.SelectedValue);
            _AdvanceInfo.EmployeeID = Convert.ToInt32(cboEmployee.SelectedValue);
            _AdvanceInfo.Position = txtPosition.Text;
            _AdvanceInfo.Date = DateTime.ParseExact(txtDate.Text, "dd/MM/yyyy", null);
            _AdvanceInfo.Amount = Convert.ToDecimal(txtAmount.Text);
            _AdvanceInfo.DinhKy = chkDinhky.Checked ? true : false;
            _AdvanceInfo.AmountPayment = Convert.ToDecimal(txtAmountMonth.Text);
            if (!chkDinhky.Checked)
            {
                txtAmountMonth.Text = txtAmount.Text;
                _AdvanceInfo.Month = 1;
            }
            else
                _AdvanceInfo.Month = Convert.ToDecimal(txtAmount.Text) / Convert.ToDecimal(txtAmountMonth.Text);
            _AdvanceInfo.Description = txtDecription.Text;
        }

        private void cboBranch_SelectedIndexChanged(object sender, EventArgs e)
        {
            UISetting.LoadDepartment(ref cboDepartment, Convert.ToInt32(cboBranch.SelectedValue));
        }

        private void cboDepartment_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadEmployee(Convert.ToInt32(cboDepartment.SelectedValue));
        }

        private void cboEmployee_SelectedIndexChanged(object sender, EventArgs e)
        {
            loadPosition(Convert.ToInt32(cboEmployee.SelectedValue));
        }

        bool CheckValid()//kiểm tra dữ liệu nhập
        {

            if (Convert.ToInt32(cboDepartment.SelectedValue) == 0)
            {
                cboDepartment.Focus();
                return false;
            }
            if (Convert.ToInt32(cboEmployee.SelectedValue) == 0)
            {
                cboEmployee.Focus();
                return false;
            }
            if (!CheckTextValid.IsDateTime(txtDate.Text, "dd/MM/yyyy"))
            {
                txtDate.Focus();
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtAmount.Text))
            {
                txtAmount.Focus();
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtAmountMonth.Text))
            {
                txtAmountMonth.Focus();
                return false;
            }
            if (!CheckTextValid.IsDecimal(txtMonth.Text))
            {
                txtMonth.Focus();
                return false;
            }
            return true;
        }

        private void btnNew_Click(object sender, EventArgs e)
        {
            txtAmount.Text = "";
            txtDate.Text = "";
            txtDecription.Text = "";
            txtAmountMonth.Enabled = false;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!CheckValid())
            {
                isChange = true;
                return;
            }
            if (CheckValid())
            {
                _wcfAdvance = Service.AdvancePayment();

                Form2Info();
                if (_AdvanceID > 0)//update
                {
                    if (!listRole.Update)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyencapnhat"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    if (_wcfAdvance.AdvancePayment_Update(_AdvanceInfo))
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("capnhatthanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                        _wcfAdvance.Close();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("capnhatkhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
                else//insert
                {
                    if (!listRole.Create)
                    {
                        MessageBox.Show(UpdateUI.GetString("khongcoquyenthemmoi"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        return;
                    }
                    _AdvanceInfo.ID = _wcfAdvance.AdvancePayment_Insert(_AdvanceInfo);
                    if (_AdvanceInfo.ID > 0)
                    {
                        Program.taskbarNotifier.Show(UIParamater.TitleProject, UpdateUI.GetString("themmoithanhcong"), UIParamater.TIME_MOVE_UP, UIParamater.TIME_SHOW, UIParamater.TIME_MOVE_DOWN);
                        isChange = false;
                        dlgResult = DialogResult.OK;
                        _wcfAdvance.Close();
                    }
                    else
                    {
                        MessageBox.Show(UpdateUI.GetString("themmoikhongthanhcong"), UIParamater.TitleProject, MessageBoxButtons.OK, MessageBoxIcon.Warning);
                        isChange = true;
                    }
                }
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            //this.Dispose();
            this.Close();
        }

        private void frmAdvancePayment_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (isChange)
            {
                if (MessageBox.Show(UpdateUI.GetString("bandathaydoidulieu") + "\n" + UpdateUI.GetString("coluutruockhithoatkhong"), UIParamater.TitleProject, MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    btnSave_Click(null, null);
                    if (isChange)
                        e.Cancel = true;
                    else
                        e.Cancel = false;
                }
            }
            this.DialogResult = dlgResult;
        }

        private void frmAdvancePayment_KeyUp(object sender, KeyEventArgs e)
        {
            switch (e.KeyCode)
            {
                case Keys.Escape:
                    this.Dispose();
                    this.Close();
                    break;
                case Keys.F2:
                    btnNew_Click(null, null);
                    break;
                case Keys.F3:
                    btnSave_Click(null, null);
                    break;
                case Keys.Enter:
                    if (!e.Alt && !e.Control)
                    {
                        this.ProcessTabKey(!e.Shift);
                        e.Handled = true;
                    }
                    break;
            }
        }

        private void txtAmount_Leave(object sender, EventArgs e)
        {
            txtAmount.Text = UISetting.GetDecimalNo(txtAmount.Text).ToString("#,##0.0");
            if (chkDinhky.Checked)
            {
                txtAmountMonth.Enabled = true;
                txtAmountMonth.Text = UISetting.GetDecimalNo(txtAmount.Text).ToString("#,##0.0");
            }
            else
            {
                txtAmountMonth.Enabled = false;
                txtAmountMonth.Text = "0.0";
            }
            GetMonthValue();
        }

        private void chkDinhky_CheckedChanged(object sender, EventArgs e)
        {
            if (chkDinhky.Checked)
            {
                txtAmountMonth.Enabled = true;
                txtAmountMonth.Text = UISetting.GetDecimalNo(txtAmount.Text).ToString("#,##0.0");
            }
            else
            {
                txtAmountMonth.Enabled = false;
                txtAmountMonth.Text = "0.0";
            }
            GetMonthValue();
        }

        private void txtAmountMonth_Leave(object sender, EventArgs e)
        {
            txtAmountMonth.Text = UISetting.GetDecimalNo(txtAmountMonth.Text).ToString("#,##0.0");
            GetMonthValue();
        }
        void GetMonthValue()
        {
            if (UISetting.GetDecimalNo(txtAmount.Text) > 0 && UISetting.GetDecimalNo(txtAmountMonth.Text) > 0)
                txtMonth.Text = (UISetting.GetDecimalNo(txtAmount.Text) / UISetting.GetDecimalNo(txtAmountMonth.Text)).ToString("#,##0.0");
            else
                txtMonth.Text = "0.0";
        }

        private void DataChange(object sender, EventArgs e)
        {
            isChange = true;
        }
    }
}
